<%--
  Created by IntelliJ IDEA.
  User: 刘松
  Date: 2020/5/4
  Time: 17:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE HTML>
<html>
	<head>
		<title>制度发布</title>
		<meta charset="UTF-8">
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<link rel="stylesheet" href="/lib/layui/layui/css/layui.css">
		<link rel="stylesheet" href="/lib/zTree_v3/css/zTreeStyle/zTreeStyle.css">
		<link rel="stylesheet" href="/lib/layui/layui/css/style.css?20200325.1">
		<link rel="stylesheet" href="/lib/layui/layui/css/common.css">
		
		<script type="text/javascript" src="/js/jquery/jquery-1.9.1.js"></script>
		<script src="/lib/jquery.form.min.js"></script>
		<script src="/lib/layui/layui/layui.js"></script>
		<script src="/lib/layui/layui/js/common.js" type="text/javascript" charset="utf-8"></script>
		<script src="/lib/layui/layui/js/baseCode/base.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript" src="/lib/layui/layui/global.js?20191126"></script>
		<script src="/js/base/base.js" type="text/javascript" charset="utf-8"></script>
		<script src="/js/jquery/jquery.cookie.js"></script>
		<style>
			.layui-treeSelect .ztree li span.button.switch {
				position: relative;
				top: 0 !important;
			}
			
			.divShow:hover .operationDiv {
				display: block;
			}
			
			.layui-form-label {
				padding: 9px 10px;
				width: 85px;
			}
			
			.searchtop {
				margin: 0 auto;
			}
			
			.layui-btn-lg {
				height: 36px;
				line-height: 36px;
				padding: 0 25px;
				position: relative;
				font-size: 16px;
				left: 2px;
				top: -1px;
			}
			
			.search {
				width: 97%;
				margin-top: 10px;
				text-align: right;
			}
			
			.xiala {
				position: relative;
				top: 3px;
				margin-left: 6px;
				height: 38px;
				line-height: 38px;
				border-radius: 2px;
				color: #666;
				border: 1px solid #d6d6d6;
				width: 200px;
				padding-left: 5px;
			}
			
			.leixing {
				margin: 10px 0;
			}
		</style>
	</head>
	<body>
		<div class="container">
			<div class="header">
				<div class="headImg" style="padding-top: 10px">
					<span style="font-size:22px;margin-left:10px;color:#494d59;margin-top: 2px"><img style="margin-left:1.5%"
					                                                                                 src="../img/commonTheme/theme6/icon_summary.png"
					                                                                                 alt=""><span style="margin-left: 10px">制度发布</span></span>
				</div>
			</div>
			
			<table id="demo" lay-filter="test"></table>
		
		</div>
		<%--自定义事件--%>
		<script type="text/html" id="doSth">
			
			{{#  if(d.instStatus == '1' && d.approveStatus == '1'){ }}
			<a class="layui-btn layui-btn-xs" lay-event="edit1">修编</a>
			{{#  } }}
			
			{{#  if((d.approveStatus == '2' || d.approveStatus == '3') && d.ownerUser == userId){ }}
			<a class="layui-btn layui-btn-xs" lay-event="edit2">修改</a>
			<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
			<a class="layui-btn layui-btn-xs" lay-event="submit">提交审批</a>
			{{#  } }}
			
			<a class="layui-btn layui-btn-xs" lay-event="history">历史版本</a>
		</script>
		
		<script type="text/html" id="toolbarDemo">
			<input type="text" style="width: 150px; display: inline-block;" class="layui-input search_name">
			<button class="layui-btn layui-btn-sm" lay-event="search">查询</button>
			<button class="layui-btn layui-btn-sm" lay-event="add">新增</button>
		</script>
	</body>
	<script>
        var userId = $.cookie('userId');

        var dept_id = '';
        var priv_id = '';
        var user_id = '';

        var table, form, laydate, layer, layedit, upload, treeSelect, transfer, util, xmSelect;
        var tableIns, demo2;
        var viewUserTypeSelect = null;

        // 选择部门控件
        $(document).on('click', '.select_dept', function () {
            dept_id = $(this).parent().attr('p_id');
            var sigle = $(this).attr('isSigle') == 1 ? '?0' : '';
            $.popWindow('/common/selectDept' + sigle);
        });
        // 清空部门
        $(document).on('click', '.clear_dept', function () {
            var eleId = '#' + $(this).parent().attr('p_id');
            $(eleId).val('');
            $(eleId).attr('deptid', '');
            $(eleId).attr('deptname', '');
        });

        // 选择角色控件
        $(document).on('click', '.select_role', function () {
            priv_id = $(this).parent().attr('p_id');
            var sigle = $(this).attr('isSigle') == 1 ? '?0' : '';
            $.popWindow('/common/selectPriv' + sigle);
        });
        // 清空角色
        $(document).on('click', '.clear_role', function () {
            var eleId = '#' + $(this).parent().attr('p_id');
            $(eleId).val('');
            $(eleId).attr('privid', '');
            $(eleId).attr('userpriv', '');
        });

        // 选择人员
        $(document).on('click', '.select_user', function () {
            user_id = $(this).parent().attr('p_id');
            var sigle = $(this).attr('isSigle') == 1 ? '?0' : '';
            $.popWindow("../../common/selectUser" + sigle);
        });
        // 清空人员
        $(document).on('click', '.clear_user', function () {
            var eleId = '#' + $(this).parent().attr('p_id');
            $(eleId).val('');
            $(eleId).attr('user_id', '');
            $(eleId).attr('username', '');
            $(eleId).attr('dataid', '');
            $(eleId).attr('userprivname', '');
        });

        //选择流程  （设计流程）
        function selectFlow() {
            layer.open({
                type: 1,
                isOutAnim: true,
                maxmin: false,
                area: ['70%', '90%'],
                title: '选择关联流程',
                btnAlign: 'c',
                btn: ['确定'],
                shade: 0,
                content: '<div class="cont" style="display: flex; margin-top: 10px ">' +
                    '   <div id="test4" class="demo-transfer" style=" display: block;margin: 0 auto; "></div>\n' +
                    '</div>',
                success: function (layero, index) {
                    var id = layero.selector;
                    $(id).find('a[class="layui-layer-btn0"]').attr('lay-demotransferactive', 'getData');

                    $.get('/flow/selAllType', function (res) {
                        var obj = res.obj;

                        //初始化右侧数据
                        var flowIds = $('#flowId').attr('flowIds');
                        var value = [];
                        if (flowIds != undefined) {
                            value = flowIds.split(',');
                        }
                        //渲染穿梭框
                        transfer.render({
                            elem: '#test4'
                            , data: obj
                            , id: 'key123'
                            , title: ['未选流程', '已选流程']
                            , showSearch: true
                            , value: value
                            , onchange: function (obj, index) {

                            }
                            , parseData: function (res) {
                                return {
                                    "value": res.flowId //数据值
                                    , "title": res.flowName //数据标题
                                    , "disabled": res.disabled  //是否禁用
                                    , "checked": res.checked //是否选中
                                }
                            }
                            , width: 430
                            , height: 550
                        })
                    });
                },
                yes: function (index, layero) {
                    var getData = transfer.getData('key123'); //获取右侧数据

                    var values = '';
                    var titles = '';

                    for (var key = 0; key < getData.length; key++) {
                        values += getData[key].value + ",";
                        titles += getData[key].title + ",";
                    }

                    $('#flowId').attr('flowIds', values);
                    $('#flowId').val(titles);
                    var text = $('#flowId').val();
                    var reg = /[,，]/g;
                    text = text.replace(reg, ",\n");
                    $('#flowId').val(text);
                    layer.close(index);
                }
            });
        }

        function delFlow() {
            $('#flowId').removeAttr('flowIds');
            $('#flowId').val('');
        }

        //下拉树
        function selectTree(tree, obj, title) {
            treeSelect.render({
                elem: tree,     // 选择器
                data: '/InstitutionSort/findSortDataTree',  // 数据
                type: 'get',      // 异步加载方式：get/post，默认get
                placeholder: '请选择',   // 占位符
                search: true,  // 是否开启搜索功能：true/false，默认false
                style: {   // 一些可定制的样式
                    folder: {enable: true},
                    line: {enable: true}
                },
                click: function (d) {     // 点击回调
                    $(tree).val(d.current.id);
                    form.render();
                },
                // 加载完成后的回调函数
                success: function (d) {
                    //回显下拉树
                    if (obj != undefined && obj != '') {
                        var treeId = $('.layui-treeSelect-body').attr('id');
                        treeSelect.checkNode('#tree', treeId, obj.institutionSort.sortId);

                        //赋值属性
                        $('input[unselect="unselect"]').attr('treenodeid', obj.institutionSort.sortId);
                    }
                    form.render();

                    if (title == '制度修编') {
                        //分类禁用
                        $('#tree').next().attr('style', 'pointer-events: none;');
                        $('input[unselect="unselect"]').addClass('layui-disabled');
                    }

                }
            });
        }

        //新增修改操作
        function enidOrInsert(title, obj) {
            layer.open({
                type: 1,
                isOutAnim: true,
                maxmin: true,
                area: ['70%', '90%'],
                title: title,
                btnAlign: 'c',
                btn: ['确定', '关闭'],
                content: ['<form class="layui-form"  lay-filter="formTest" style="margin: 2%;">' +
                '<input type="text" name="instId" style="display: none;" >' +
                '<div class="layui-row">' +
                ' <div class="layui-col-xs6">',
                    ' <div class="layui-form-item">\n' +
                    '    <label class="layui-form-label">排序号</label>\n' +
                    '    <div class="layui-input-block">\n' +
                    '      <input type="text" name="instNo" lay-verify="instNo" autocomplete="off" placeholder="请输入排序号（数字）" class="layui-input">\n' +
                    '    </div>\n' +
                    '  </div>',
                    ' <div class="layui-form-item">\n' +
                    '    <label class="layui-form-label">制度文档编号</label>\n' +
                    '    <div class="layui-input-block">\n' +
                    '      <input id="instNumbers" type="text" name="instNumber" lay-verify="instNumber" autocomplete="off" placeholder="请输入制度编号" class="layui-input">\n' +
                    '    </div>\n' +
                    '  </div>',
                    ' <div class="layui-form-item">\n' +
                    '    <label class="layui-form-label">制度文档名称</label>\n' +
                    '    <div class="layui-input-block">\n' +
                    '      <input type="text" name="instName" lay-verify="instName" autocomplete="off" placeholder="请输入文档名称" class="layui-input">\n' +
                    '    </div>\n' +
                    '  </div>' +
                    ' <div class="layui-form-item">\n' +
                    '    <label class="layui-form-label">制度文档分类</label>\n' +
                    '    <div class="layui-input-block">\n' +
                    '      <select name="sortId"></select>' +
                    '    </div>\n' +
                    '  </div>',
                    ' <div class="layui-form-item">\n' +
                    '    <label class="layui-form-label">关键字</label>\n' +
                    '    <div class="layui-input-block">\n' +
                    '      <input type="text" name="keyWords" lay-verify="title" autocomplete="off" placeholder="请输入关键字" class="layui-input">\n' +
                    '    </div>\n' +
                    '  </div>',
                    ' <div class="layui-form-item">\n' +
                    '    <div class="layui-inline-block">' +
                    '    <label class="layui-form-label">所属单位</label>\n' +
                    '      <div class="layui-input-block">\n' +
                    '        <input type="text" name="belongtoUnit" id="belongToUnit" placeholder="所属单位" class="layui-input" readonly>\n' +
                    '      </div>\n' +
                    '    </div>' +
                    '  </div>',
                    '</div><div class="layui-col-xs6">',
                    ' <div class="layui-form-item">\n' +
                    '    <div class="layui-inline-block">' +
                    '    <label class="layui-form-label" style="width: 105px;padding-left: 0;box-sizing: border-box;">创建/修编人员</label>\n' +
                    '      <div class="layui-input-inline">\n' +
                    '        <input type="text" id="ownerUser" placeholder="创建/修编人员" class="layui-input" readonly>\n' +
                    '      </div>\n' +
                    '    </div>' +
                    '  </div>',
                    ' <div class="layui-form-item">\n' +
                    '    <label class="layui-form-label">人员类别</label>\n' +
                    '    <div class="layui-input-block">\n' +
                    '       <div id="viewUserTypeSelect" class="xm-select-demo" style="width: 100%;"></div>'+
                    '    </div>\n' +
                    '  </div>',
                    ' <div class="layui-form-item">\n' +
                    '    <div class="layui-inline-block">' +
                    '    <label class="layui-form-label">制度查看部门</label>\n' +
                    '      <div class="layui-input-inline" style="width: 55%;">\n' +
                    '        <input type="text" name="viewDepts" id="viewDepts" placeholder="制度查看部门" class="layui-input" readonly>\n' +
                    '      </div>\n' +
                    '    </div>' +
                    '    <div class="layui-form-mid layui-word-aux">' +
                    '      <div style="color:#1687CB" p_id="viewDepts">\n' +
                    '        <a href ="javascript:void(0);" class="select_dept" style="color:#1687CB">选择</a>\n' +
                    '        <a href ="javascript:void(0);" class="clear_dept" style="color:#1687CB">清空</a>\n' +
                    '      </div>\n' +
                    '    </div>' +
                    '  </div>',
                    ' <div class="layui-form-item">\n' +
                    '    <div class="layui-inline-block">' +
                    '    <label class="layui-form-label">制度查看角色</label>\n' +
                    '      <div class="layui-input-inline" style="width: 55%;">\n' +
                    '        <input type="text" name="viewPrivs" id="viewPrivs" placeholder="制度查看角色" class="layui-input" readonly>\n' +
                    '      </div>\n' +
                    '    </div>' +
                    '    <div class="layui-form-mid layui-word-aux">' +
                    '      <div style="color:#1687CB" p_id="viewPrivs">\n' +
                    '        <a href ="javascript:void(0);" class="select_role" style="color:#1687CB">选择</a>\n' +
                    '        <a href ="javascript:void(0);" class="clear_role" style="color:#1687CB">清空</a>\n' +
                    '      </div>\n' +
                    '    </div>' +
                    '  </div>',
                    ' <div class="layui-form-item">\n' +
                    '    <div class="layui-inline-block">' +
                    '    <label class="layui-form-label">制度查看人员</label>\n' +
                    '      <div class="layui-input-inline" style="width: 55%;">\n' +
                    '        <input type="text" name="viewUsers" id="viewUsers" placeholder="制度查看人员" class="layui-input" readonly>\n' +
                    '      </div>\n' +
                    '    </div>' +
                    '    <div class="layui-form-mid layui-word-aux">' +
                    '      <div style="color:#1687CB" p_id="viewUsers">\n' +
                    '        <a href ="javascript:void(0);" class="select_user" style="color:#1687CB">选择</a>\n' +
                    '        <a href ="javascript:void(0);" class="clear_user" style="color:#1687CB">清空</a>\n' +
                    '      </div>\n' +
                    '    </div>' +
                    '  </div>',
                    ' <div class="layui-form-item">\n' +
                    '    <div class="layui-inline-block">' +
                    '    <label class="layui-form-label">所属部门</label>\n' +
                    '      <div class="layui-input-inline" style="width: 55%;">\n' +
                    '        <input type="text" name="belongtoDepts" id="belongToDepts" placeholder="请选择所属部门" class="layui-input" readonly>\n' +
                    '      </div>\n' +
                    '    </div>' +
                    '    <div class="layui-form-mid layui-word-aux">' +
                    '      <div style="color:#1687CB" p_id="belongToDepts">\n' +
                    '        <a href ="javascript:void(0);" class="select_dept" style="color:#1687CB" isSigle="1">选择</a>\n' +
                    '        <a href ="javascript:void(0);" class="clear_dept" style="color:#1687CB">清空</a>\n' +
                    '      </div>\n' +
                    '    </div>' +
                    '  </div>',
                    '</div></div>',
                    ' <div id="instContents" class="layui-form-item" style="display: block;">\n' +
                    '    <label class="layui-form-label">制度正文</label>\n' +
                    '    <div class="layui-input-block">\n' +
                    '      <textarea id="demo2" style="display: none;" nameType="制度"></textarea>\n' +
                    '    </div>\n' +
                    '  </div>',
                    ' <div class="layui-form-item" style="width: 47%;">\n' +
                    '    <label class="layui-form-label">版本号</label>\n' +
                    '    <div class="layui-input-block">\n' +
                    '      <input type="text" name="version" lay-verify="title" autocomplete="off" placeholder="请输入版本号" class="layui-input">\n' +
                    '    </div>\n' +
                    '  </div>',
                    ' <div id="Explanation" class="layui-form-item" style="width: 47%; display: none;">\n' +
                    '    <label class="layui-form-label">修编说明</label>\n' +
                    '    <div class="layui-input-block">\n' +
                    '      <textarea name="editContent" placeholder="请输入内容" class="layui-textarea" ></textarea>\n' +
                    '    </div>\n' +
                    '  </div>' +
                    ' <div class="layui-form-item">\n' +
                    '    <div class="layui-inline-block">' +
                    '    <label class="layui-form-label">关联流程</label>\n' +
                    '      <div class="layui-input-inline" style="width: 490px;">\n' +
                    '        <textarea type="text" name="flowId" id="flowId" placeholder="请选择流程"  class="layui-textarea area" readonly></textarea>\n' +
                    '      </div>\n' +
                    '    </div>' +
                    '    <div class="layui-form-mid layui-word-aux" style="width: 12%;">' +
                    '      <div style="color:#1687CB;">\n' +
                    '        <a href ="javascript:void(0);" onclick="selectFlow()" style="color:#1687CB" >选择</a>\n' +
                    '        <a href ="javascript:void(0);" onclick="delFlow()" style="color:#1687CB">清空</a>\n' +
                    '      </div>\n' +
                    '    </div>' +
                    '  </div>',
                    ' <div class="layui-form-item">\n' +
                    '    <div class="layui-inline-block">' +
                    '    <label class="layui-form-label">审批人</label>\n' +
                    '      <div class="layui-input-inline" style="width: 490px;">\n' +
                    '        <input type="text" name="approvalUser" id="approvalUser" placeholder="请选择审批人" class="layui-input" readonly>\n' +
                    '      </div>\n' +
                    '    </div>' +
                    '    <div class="layui-form-mid layui-word-aux" style="width: 12%;">' +
                    '      <div style="color:#1687CB" p_id="approvalUser">\n' +
                    '        <a href ="javascript:void(0);" class="select_user" style="color:#1687CB" isSigle="1">选择</a>\n' +
                    '        <a href ="javascript:void(0);" class="clear_user" style="color:#1687CB">清空</a>\n' +
                    '      </div>\n' +
                    '    </div>' +
                    '  </div>',
                    '</form>'].join(''),
                success: function (layero, index) {
					
                    // 获取当前登录人信息
                    $.get('/getLoginUser', function (res) {
                        if (res.flag && res.object) {
                            $('#ownerUser').val(res.object.userName);
                        }
                    });

                    // 人员类别
                    $.get('/code/getCode?parentNo=staffType', function (res) {
                        var codeData = res.obj;
                        
                        viewUserTypeSelect = xmSelect.render({
                            el: '#viewUserTypeSelect',
                            toolbar: {
                                show: true,
                            },
	                        name: 'viewUserType',
                            autoRow: true,
	                        prop: {
                                name: 'codeName',
                                value: 'codeNo',
                            },
                            data: codeData
                        });

                        if (obj != undefined && obj != '') {
                            // 人员类别
                            var viewUserTypeArr = (obj.viewUserType || '').split(',');
                            viewUserTypeSelect.setValue(viewUserTypeArr);
                        }

                    });
                    
                    
                    $.get('/InstitutionSort/getSortData', function(res){
                        var sortStr = '<option value="">请选择</option>';
                        if (res.flag) {
                            $('#belongToUnit').val(res.object.deptName);
                            $('#belongToUnit').attr('deptid', res.object.deptId);
                            res.obj.forEach(function(item){
                                sortStr += '<option value="'+item.sortId+'">'+item.sortName+'</option>'
                            });
                        }
                        $('select[name="sortId"]').html(sortStr);
	                    if (obj != undefined && obj != '') {
	                        $('select[name="sortId"]').val(obj.sortId);
                        }
                        form.render();
                    });

                    //判断是否显示修编说明
                    if (title == '制度修编') {
                        $('#Explanation').css('display', 'block');

                        //排序号禁用
                        $('input[name="instNo"]').attr('disabled', 'disabled');
                        $('input[name="instNo"]').addClass('layui-disabled');

                        //编号禁用
                        $('input[name="instNumber"]').attr('disabled', 'disabled');
                        $('input[name="instNumber"]').addClass('layui-disabled');

                        //名称禁用
                        $('input[name="instName"]').attr('disabled', 'disabled');
                        $('input[name="instName"]').addClass('layui-disabled');

                    }

                    //修改的时候不可以修改编号
                    if (title == '制度编辑') {
                        //编号禁用
                        $('input[name="instNumber"]').attr('disabled', 'disabled');
                        $('input[name="instNumber"]').addClass('layui-disabled');

                    }

                    //编辑器上传图片
                    layedit.set({
                        uploadImage: {
                            url: '/upload?module=institution',
                            type: 'post'
                        }
                    });

                    //建立编辑器
                    demo2 = layedit.build('demo2');

                    //内容是否回显
                    if (obj != undefined && obj != '') {
                        //给表单赋值
                        form.val("formTest", {
                            "instId": obj.instId,
                            "instNo": obj.instNo,
                            "instNumber": obj.instNumber,
                            "instName": obj.instName,
                            "keyWords": obj.keyWords,
                            "version": obj.version,
                            "approvalUser": obj.approvalUserName,
                            "editContent": obj.editContent,
                            "flowId": obj.flowName,
	                        "sortId": obj.sortId,
	                        "viewDepts": obj.viewDeptsName || '',
	                        "viewPrivs": obj.viewPrivsName || '',
	                        "viewUsers": obj.viewUsersName || '',
	                        "belongtoDepts": obj.belongtoDeptsName || '',
	                        "belongtoUnit": obj.belongtoUnitName || ''
	                        // "viewUserType": obj.viewUserType || '1'
                        });
                        
						$('#belongToUnit').attr('deptid', obj.belongtoUnit || '');
                        // 制度查看部门
                        $('#viewDepts').attr('deptid', obj.viewDepts || '');
                        // 制度查看角色
                        $('#viewPrivs').attr('userpriv', obj.viewPrivs || '');
                        // 制度查看人员
                        $('#viewUsers').attr('user_id', obj.viewUsers || '');
                        // 所属部门
                        $('#belongToDepts').attr('deptid', obj.belongtoDepts || '');

                        //选人控件回显
                        $('#approvalUser').attr('user_id', obj.approvalUser);

                        //判断回显附件还是编辑器
                        if (obj.instType == '1' || obj.instType == 1) {
                            $('#instContents').show();
                            $('#instAttachment').hide();
                            layedit.setContent(demo2, obj.instContent);
                        }

                        //关联流程的回显
                        $('#flowId').attr('flowIds', obj.flowId);

                        //停用下的未批准不可以修改版本号
                        if (obj.instStatus == '0' && obj.approveStatus == '2') {
                            $('input[name="version"]').attr('disabled', 'disabled');
                            $('input[name="version"]').addClass('layui-disabled');
                        }

                    }

                    form.render();
                },
                yes: function (index, layero) {
                    //获取表单区域所有值
                    var data1 = form.val("formTest");

                    //制度分类
                    // data1['sortId'] = $('input[unselect="unselect"]').attr('treenodeid');
                    if (!data1.sortId) {
                        layer.msg('请选择制度分类', {icon: 0, time: 1000});
                        return false;
                    }

                    //审批人
                    data1['approvalUser'] = $('#approvalUser').attr('user_id');
                    if (data1['approvalUser'] === undefined) {
                        layer.msg('请选择审批人', {icon: 0, time: 1000});
                        return false;
                    }
                    
                    // 制度查看部门
	                data1['viewDepts'] = $('#viewDepts').attr('deptid') || '';
                    // 制度查看角色
	                data1['viewPrivs'] = $('#viewPrivs').attr('userpriv') || '';
	                // 制度查看人员
	                data1['viewUsers'] = $('#viewUsers').attr('user_id') || '';
	                // 所属部门
	                data1['belongtoDepts'] = $('#belongToDepts').attr('deptid') || '';
	                // 人员类别
	                data1['viewUserType'] = viewUserTypeSelect.getValue('valueStr');
	                data1['belongtoUnit'] = $('#belongToUnit').attr('deptid');

                    //附件数量
                    data1['fileNum'] = 0;

                    //制度类型
                    data1['instType'] = 1;

                    //制度内容和附件
                    if (data1['instType'] === 1 || data1['instType'] === '1') {
                        data1['instContent'] = layedit.getContent(demo2);
                    }

                    //关联流程
                    if (data1['flowId'] != '') {
                        data1['flowId'] = $('#flowId').attr('flowIds');
                    }

                    var url = '/InstitutionContent/IntertContent';
                    //判断新增还是修改
                    if (title == '制度编辑') {
                        url = '/InstitutionContent/editContent';
                        data1['isEdit'] = '编辑';
                    } else if (title == '制度修编') {
                        url = '/InstitutionContent/editContent';
                        data1['isEdit'] = '修编';
                    }

                    $.post(url, data1, function (res) {
                        if (res.flag) {
                            layer.msg('保存成功', {icon: 1, time: 1000}, function () {
                                table.reload('demo', {
                                    page: {
                                        curr: 1
                                    }
                                });
                                layer.close(index);
                            });
                        } else {
                            layer.msg(res.msg, {icon: 2, time: 1000});
                        }
                    });
                },
                btn2: function (index, layero) {
                    layer.close(index);
                }
            });
        }

        //选择URL
        function institutionURL() {
            layer.open({
                type: 1,
                resize: true,
                offset: 'auto',
                area: ['65%', '60%'],
                title: '选择制度URL',
                btn: ['确定', '关闭'],
                btnAlign: 'c',
                content: '<div>' +
                    '<form class="layui-form" action="" style="margin-top: 10px">' +
                    ' <div class="layui-form-item">\n' +
                    '    <div class="layui-inline">\n' +
                    '      <label class="layui-form-label">所属分类</label>\n' +
                    '      <div class="layui-input-inline" style="width: 150px">\n' +
                    '        <input type="text" id="treeType" class="layui-input">\n' +
                    '      </div>\n' +
                    '    </div>\n' +
                    '    <div class="layui-inline">\n' +
                    '      <label class="layui-form-label" style="width:30px">名称</label>\n' +
                    '      <div class="layui-input-inline" style="width: 150px">\n' +
                    '        <input type="text" id="className" name="email" lay-verify="email" autocomplete="off" class="layui-input">\n' +
                    '      </div>\n' +
                    '    </div>\n' +
                    '    <div class="layui-inline">\n' +
                    '      <button type="button" id="btn" class="layui-btn layui-btn-sm">搜索</button>\n' +
                    '    </div>\n' +
                    '  </div>' +
                    '</form>' +
                    '<table id="demoURL" lay-filter="test"></table>' +
                    '</div>',
                success: function (layero, index) {
                    $("#btn").click(function () {
                        //类型
                        var INST_TYPE = $("#kind").val();
                        //所属分类
                        var sortId = $("#treeType").val();
                        //名称
                        var instName = $("#className").val();
                        search.reload({
                            url: '/InstitutionContent/findContenHTMLORFILEURL',
                            where: {
                                instName: instName,
                                sortId: sortId,
                                INST_TYPE: INST_TYPE,
                                type: 1

                            },
                            page: {
                                curr: 1
                            }
                        })
                    })


                    //渲染表格
                    var search = table.render({
                        elem: '#demoURL'
                        , height: 312
                        , url: '/InstitutionContent/findContenHTMLORFILEURL' //数据接口
                        , page: false //开启分页
                        , cols: [[ //表头
                            {type: 'radio'}
                            , {field: 'attachName', title: '名称'}
                            , {field: 'attUrl', title: 'URL'}
                        ]],
                        parseData: function (res) {
                            return {
                                code: 0,
                                msg: res.msg,
                                count: res.totleNum,
                                data: res.obj
                            }
                        }

                    });

                    //渲染下拉树结构
                    selectTree('#treeType');

                    form.render('select');
                },
                yes: function (index, layero) {
                    var checkStatus = table.checkStatus('demoURL'); //idTest 即为基础参数 id 对应的值
                    var obj = checkStatus.data[0];
                    if (obj == undefined) {
                        layer.msg('请选择一个URL', {icon: 6});
                        return false;
                    }
                    $('input[name="url"]').val(obj.attUrl);
                    layer.close(index);
                }
            });
        }

        $(function () {
            layui.use(['table', 'form', 'laydate', 'element', 'layer', 'layedit', 'upload', 'treeSelect', 'transfer', 'util', 'xmSelect'], function () {
                table = layui.table,
                    form = layui.form,
                    laydate = layui.laydate,
                    layer = layui.layer,
                    layedit = layui.layedit,
                    upload = layui.upload,
                    treeSelect = layui.treeSelect,
                    transfer = layui.transfer,
                    util = layui.util,
                    xmSelect = layui.xmSelect;

                //表格渲染
                tableIns = table.render({
                    elem: '#demo',
                    url: '/InstitutionContent/getByBelongDept',
                    where: {instType: '1'},
                    parseData: function (res) {
                        return {
                            code: 0,
                            msg: res.msg,
                            count: res.totleNum,
                            data: res.obj
                        }
                    },
                    page: true,
                    toolbar: '#toolbarDemo',
                    cols: [[
                        {
                            field: 'instName', title: '名称', align: 'center',
                            templet: function (d) {
                                return '<a href="javascript:;" style="text-decoration:underline;color:blue">' + d.instName + '</a>'
                            }, event: 'title'

                        }
                        , {field: 'instNumber', title: '编号', align: 'center'}
                        , {
                            field: 'instStatus', title: '使用状态', align: 'center', templet: function (d) {
                                if (d.instStatus == 1) {
                                    return "生效"
                                } else {
                                    return "停用"
                                }
                            }
                        }
                        , {
                            field: 'approveStatus', title: '审核状态', align: 'center', templet: function (d) {
                                if (d.approveStatus == 0) {
                                    return "待审批"
                                } else if (d.approveStatus == 1) {
                                    return "已批准"
                                } else if (d.approveStatus == 2) {
                                    return "未批准"
                                } else if (d.approveStatus == 3) {
                                    return "未提交"
                                }
                            }
                        },
                        {field: 'version', title: '当前版本', align: 'center'},
                        {title: '操作', align: 'right', toolbar: '#doSth', width: 300}
                    ]]
                });

                //监听事件头
                table.on('toolbar(test)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            enidOrInsert('制度文档');
                            break;
                        case 'search':
                            var val = $('.search_name').val().trim();

                            tableIns.reload({
                                where: {
                                    instType: '1',
                                    instName: val
                                },
                                done: function () {
                                    $('.search_name').val(val);
                                }
                            });
                            break;
                    }
                });

                //监听工具条事件
                table.on('tool(test)', function (obj) {
                    var event = obj.event;
                    var data = obj.data;
                    if (event == 'submit') {
                        // 提交审批执行的操作
                        layer.confirm('确定要提交吗？', {icon: 3, title: '提示'}, function (index) {
                            $.ajax({
                                type: 'get',
                                url: '/InstitutionContent/editContent',
                                data: {
                                    instId: data.instId,
                                    approveStatus: 0,
                                    isEdit: '编辑'
                                },
                                success: function (res) {
                                    if (res.flag) {
                                        layer.msg('提交成功', {icon: 1, time: 1000});
                                        table.reload('demo', {
                                            page: {
                                                curr: 1
                                            }
                                        });
                                        layer.close(index);
                                    } else {
                                        layer.msg('提交失败', {icon: 2, time: 1000});
                                    }
                                }
                            });
                        });
                    } else if (event == "history") {
                        //历史版本
                        window.open("/institution/historicalVersion?instId=" + data.instId);
                    } else if (event == "title") {
                        // 查看详情
                        if (data.instType == 1) {
                            layer.open({
                                type: 2,
                                isOutAnim: true,
                                maxmin: true,
                                title: '制度详情',
                                area: ['100%', '100%'],//宽高
                                btnAlign: 'c',
                                btn: ['返回'],
                                content: '/institution/selectInstitution?instId=' + data.instId
                                , yes: function (index) {
                                    layer.close(index);
                                }
                            });
                        }
                    } else if (event == "del") {
                        //删除
                        layer.confirm('确定要删除吗?', {icon: 3, title: '提示'}, function (index) {
                            $.ajax({
                                type: 'get',
                                url: '/InstitutionContent/deleteContentByInstId',
                                data: {
                                    instNumber: data.instNumber
                                },
                                success: function (res) {
                                    if (res.flag) {
                                        layer.msg('删除成功', {icon: 1, time: 1000});
                                        obj.del();
                                        layer.close(index);
                                        table.reload('demo', {
                                            page: {
                                                curr: 1
                                            }
                                        });
                                    } else {
                                        layer.msg('删除失败', {icon: 2, time: 1000});
                                    }
                                }
                            });
                        });
                    } else if (event == "edit2") {
                        //修改
                        enidOrInsert('制度编辑', data);
                    } else if (event == "edit1") {
                        //修编
                        enidOrInsert('制度修编', data);
                    }
                });
            });
        });
	</script>

</html>
